import Vue from 'vue'
import App from './App.vue'
import {
    MessageBox,
    Message,
    Button,
    Radio,
    Container,
    Header,
    Aside,
    Menu,
    Submenu,
    MenuItem,
    MenuItemGroup,
    Dropdown,
    DropdownMenu,
    DropdownItem,
    Main,
    Row,
    Col,
    Card,
    Table,
    TableColumn,
    Breadcrumb,
    BreadcrumbItem,
    Tag,
    Form,
    FormItem,
    Input,
    Select,
    Switch,
    DatePicker,
    Option,
    Dialog,
    Pagination,
} from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import './less/index.less'

import router from "./router";
import store from './store'
import http from 'axios'
import '@/api/mock.js'

Vue.config.productionTip = false
Vue.use(Button);
Vue.use(Radio);
Vue.use(Container);
Vue.use(Header);
Vue.use(Aside);
Vue.use(Menu);
Vue.use(Submenu);
Vue.use(MenuItem);
Vue.use(MenuItemGroup);
Vue.use(Dropdown);
Vue.use(DropdownMenu);
Vue.use(DropdownItem);
Vue.use(Main);
Vue.use(Row);
Vue.use(Col);
Vue.use(Card);
Vue.use(Table);
Vue.use(TableColumn);
Vue.use(Breadcrumb);
Vue.use(BreadcrumbItem);
Vue.use(Tag);
Vue.use(Form);
Vue.use(FormItem);
Vue.use(Input);
Vue.use(Select);
Vue.use(Switch);
Vue.use(DatePicker);
Vue.use(Option);
Vue.use(Dialog);
Vue.use(Pagination);


Vue.prototype.$http = http
Vue.prototype.$confirm = MessageBox.confirm
Vue.prototype.$message = Message

router.beforeEach((to, from, next) => {
    store.commit('getToken') // 清除token
    const token = store.state.user.token
    if (!token && to.name !== 'login') {
        next({ name: 'login' })
    } else if (token && to.name === 'login') { // 当处于登录状态不回返回 login登录页面
        next({ name: 'home' })
    } else {
        next()
    }
})

new Vue({
    store,
    router,
    render: h => h(App),
    // 解决直接登录home 白屏问题    
    created() {
        store.commit("addMenu", router);
    }
}).$mount('#app')